package com.tiany.kotlin_study.MyAdapter

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import com.tiany.kotlin_study.Class.Fruit
import com.tiany.kotlin_study.R


class FruitAdapter(activity: Activity, val resourceID: Int, data: List<Fruit>) :
    ArrayAdapter<Fruit>(activity, resourceID, data) {

    inner class ViewHolder(val fruitImage: ImageView,val fruitName:TextView)

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view: View
        val viewHolder:ViewHolder
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(resourceID, parent, false)
            val fruitImage: ImageView = view.findViewById(R.id.list_img)
            val fruitName: TextView = view.findViewById(R.id.list_tv)
            viewHolder=ViewHolder(fruitImage, fruitName)
            view.tag=viewHolder
        } else {
            view = convertView
            viewHolder=view.tag as ViewHolder
        }
        val fruit = getItem(position)
        if (fruit != null) {
            viewHolder.fruitImage.setImageResource(fruit.ImageID)
            viewHolder.fruitName.text = fruit.name
        }
        return view
    }


}